package edu.moravian.game;

import edu.moravian.UI.entity.BallEntityView;
import edu.moravian.UI.game.BouncingBallGameView;
import edu.moravian.entity.BallEntity;
import edu.moravian.entity.BallEntityManager;
import edu.moravian.math.Point2D;
import edu.moravian.world.World2D;
import edu.moravian.world.WorldDescriptor;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


/**
 * This "game" is the State game
 * A player controls the position of the earth with the arrow keys
 * and asteroids chase it. The asteroids cycle between chasing, eating
 * and resting. The game is over when the user quits or the earth has been 
 * destroyed by too many asteroid hits
 * 
 * @author stejr02
 */
public class BouncingBallGame implements Game
{
    private boolean pause;
    
    private final Point2D center;

    //information about the world
    private double worldWidth;
    private double worldHeight;
    
    private boolean done;

    private List<BallEntityView> balls;
    
//    private BallEntity ballA;
//    private BallEntity ballB;

    
    private BallEntityManager ballManager;
    private BouncingBallGameView gameView;
    
    private Random rand;
    /**
     * Create the game
     *
     * @param worldWidth the width of the world
     * @param worldHeight the height of the world
     */
    public BouncingBallGame(double worldWidth, double worldHeight) 
    {
        //setup world information
        this.worldWidth = worldWidth;
        this.worldHeight = worldHeight;
        
        
        Point2D lowerLeft = new Point2D(0, 0);
        Point2D upperRight = new Point2D(worldWidth, worldHeight);
        
        WorldDescriptor world = new WorldDescriptor(lowerLeft, upperRight);
        
        this.center = new Point2D(worldWidth/2.0, worldHeight/2.0);
        
        this.done = false;
        pause = true;
        ballManager = new BallEntityManager(world);
        
        gameView = new BouncingBallGameView(world, ballManager);
        
        
        
        //ballA = new BallEntity(new Point2D(200, 200), new Vector2D(1, 1).getNormalized(), 1, 20, 20);
        //ballB = new BallEntity(new Point2D(400, 400), new Vector2D(-1, -1).getNormalized(), 1, 10, 10);
        
    }

    private double randInRange(double low, double high)
    {
        return rand.nextDouble() * (high - low) + low;
    }
    /**
     * Update the game to the next frame
     */
    @Override
    public void update(double delta)
    {
        if(!pause)
        {
            ballManager.update(delta); 
        }
       
        
        
        
    }
       

    
    /**
     * Draw the current game state
     * @param w2d the graphics to draw to
     */
    @Override
    public void draw(World2D w2d)
    {
        gameView.draw(w2d);
        
        
      
    }

    /**
     * Determine whether the game is done. 
     * 
     * @return whether the player wanted the game to end
     */
    @Override
    public boolean done()
    {
        return done;
    }

    
    
        
    /**
     * Handle for key presses to control the players motion. 
     * This will be  part of the controller for MVC
     * 
     * @param e the key event
     */
    @Override
    public void handleKeyPress(KeyEvent e)
    {
    
    }


    /**
     * Handle for key release to control the players motion.
     * This will be part of the controller for MVC
     * 
     * @param e the key event
     */
    @Override
    public void handleKeyRelease(KeyEvent e)
    {
      
    }
 

    /**
     * A handler method for key events 
     * 
     * @param e 
     */
    @Override
    public void handleKeyTyped(KeyEvent e) 
    {
        
        //actions shared between all states
        
        //'q' is the character to quit at anypoint in the game
        if(e.getKeyChar() == 'q')
        {
            done = true;
        }
        
        else if(e.getKeyChar() == ' ')
        {
            pause = !pause;
        }
        
        else if(e.getKeyChar() == 'e')
        {
            ballManager.decreaseEpsilon();
        }
        else if(e.getKeyChar() == 'E')
        {
            ballManager.increaseEpsilon();
        }
        
   
        
    }


    /**
     * Mouse movement handler
     * 
     * @param e the mouse event
     */
    @Override
    public void handleMouseMoved(Point2D mouseLocation) 
    {
        //In this game, do nothing
    }

    
}
